package com.neusoft.acquistion.sensor.consumer.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.acquistion.sensor.consumer.entity.Monitor;
import com.neusoft.acquistion.sensor.consumer.entity.SensorStatus;
import com.neusoft.acquistion.sensor.consumer.service.ISensordataService;
import org.slf4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

public class MonitorReaderThread extends  Thread {

//    保存所有的线程，构成一个线程池
    private   static final HashMap<String, MonitorReaderThread> monitorReaders = new HashMap<String, MonitorReaderThread>() ;
    private ISensordataService sensordataService;

    private Monitor monitor;

    private WebClient webClient;
    private  String path = "/monitor/all";
    private boolean running =  true;
    private int interruptedMillis = 2000;
    private ObjectMapper objectMapper = new ObjectMapper();
    private Map<String, Object> monitorMap = null;
    private  TypeReference<Map> typeReference = new TypeReference<Map>() {};

    public void setStatus(SensorStatus status) {
        this.status = status;
    }

    public SensorStatus getStatus() {
        return status;
    }

    private  SensorStatus status = SensorStatus.ONLINE;


    public MonitorReaderThread(Monitor monitor, ISensordataService sensordataService ){
        this.sensordataService = sensordataService;
        this.monitor = monitor;

        webClient =  WebClient.builder()
                .baseUrl(getUrl())
                .defaultHeader(HttpHeaders.USER_AGENT,"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)")
                .defaultCookie("ACCESS_TOKEN", "test_token")
                .build();
//        启动读取线程
        this.start();
        monitorReaders.put(monitor.getHostname(), this);
    }

    private String getUrl() {
        return  "http://" +    monitor.getHostname()+ path;
    }

    @Override
    public void run() {
        while (running){
            Mono<String> mono = webClient
                    .get() // GET 请求
                    .uri("")  // 请求路径
                    .retrieve() // 获取响应体
                    .bodyToMono(String.class); //响应数据类型转换
//            System.out.println(mono.block());

            try {
                monitorMap = objectMapper.readValue(mono.block().toString(), typeReference);
                sensordataService.wiriteMonitorData(monitorMap);
//                System.out.println(monitorMap.getClass());
//                for (Map.Entry<String, Object> entry : monitorMap.entrySet()) {
//                    System.out.println(entry.getKey() + ":" + entry.getValue());

//                }
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            try {
                Thread.sleep(interruptedMillis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void endWork(){
        running = false;
    }
}
